//
//  GroupListController.m
//  WChat
//
//  Created by lee on 2018/8/6.
//  Copyright © 2018年 mjsfax. All rights reserved.
//

#import "GroupListController.h"
#import "ChatViewController.h"

@interface GroupListController ()
@property (strong, nonatomic) NSMutableArray *groups;
@end

@implementation GroupListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
    if (!self.groups.count) {
        [self queryGroups];
    }
}

- (void)setup {
    self.title = @"群聊";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(addGroup)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([self class])];
}

- (void)addGroup {
    NSString *username = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    EMGroupStyleSetting *groupStyleSetting = [[EMGroupStyleSetting alloc] init];
    groupStyleSetting.groupMaxUsersCount = 500; // 创建500人的群，如果不设置，默认是200人。
    groupStyleSetting.groupStyle = eGroupStyle_PublicOpenJoin; // 创建不同类型的群组，这里需要才传入不同的类型
    [[EaseMob sharedInstance].chatManager asyncCreateGroupWithSubject:[NSString stringWithFormat:@"%@的群组",username]
                                                          description:@"群组描述描述描述"
                                                             invitees:nil
                                                initialWelcomeMessage:@"邀请您加入群组"
                                                         styleSetting:groupStyleSetting
                                                           completion:^(EMGroup *group, EMError *error) {
                                                               if(!error){
                                                                   NSLog(@"创建成功 -- %@",group);
                                                                   [self queryGroups];
                                                               }
                                                           } onQueue:nil];
}

- (void)queryGroups {
    [self.groups removeAllObjects];
    [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
        if (error) {
            NSLog(@"%@",error);
            return;
        }
        [self.groups removeAllObjects];
        [self.groups addObjectsFromArray:groups];
        [self.tableView reloadData];
    } onQueue:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.groups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class])];
    EMGroup *group = [self.groups objectAtIndex:indexPath.row];
    cell.textLabel.text = group.groupSubject;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    EMGroup *group = [self.groups objectAtIndex:indexPath.row];
    ChatViewController *chat = [[ChatViewController alloc] init];
    chat.username = group.groupId;
    
    UITabBarController *tab = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [tab setSelectedIndex:0];
    
    [(UINavigationController *)tab.childViewControllers[0] pushViewController:chat animated:YES];
}

- (NSMutableArray *)groups {
    if (!_groups) {
        NSArray *array = [[EaseMob sharedInstance].chatManager groupList];
        if (!array.count) {
            array = [[EaseMob sharedInstance].chatManager loadAllMyGroupsFromDatabaseWithAppend2Chat:YES];
        }
        _groups = [NSMutableArray arrayWithArray:array];
    }
    return _groups;
}

@end
